// pages/component/selectUser/selectUser.js
import {uploadFile,prePicTure} from "../../../utils/upload"
import ajax from "../../network/index"
Component({
  /**
   * 组件的属性列表
   */
  properties: {
      filecode:{
        type:String
      },
      projectCode:{
        type:String
      },
      userId:{
        type:String
      }

  },
  
  /**
   * 组件的初始数据
   */
  data: {
    selectData:[],
    content:"",
    image:[]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    btn(){

     let arr=[]
      uploadFile().then(v=>{
        v.forEach(item =>{
          arr.push(item.url)
       
        })
        this.uploadProjectFile(arr)
      })
    },
    close(){
      this.triggerEvent('close')
    },
    // 预览图片
    preView(e){
      let item = e.currentTarget.dataset.item 
           let file ={
            source:item.type,
             url:item.url
           }
           let newdata={
             currentTarget:{
               dataset:{
                 file
               }
             }
           }
           prePicTure(newdata,this.data.image)
    },
    uploadProjectFile(arr){
      ajax({
        url:"/api/projectFileClassify/addProjectFile",
        method:"post",
        data:{
          classifyCode:this.data.filecode,
          projectCode:this.data.projectCode,
          urls:arr,
          userId:this.data.userId
        }
      }).then(v=>{
        if(v.code==200){
          wx.showToast({
            title: v.msg,
          })
          this.getListFile()
        }else{
          wx.showToast({
            title: v.msg,
          })
        }
      })
    },
    //获取文件列表信息
    getListFile(){
      let image =[]
        ajax({
          url:"/api/projectFileClassify/selectProjectFileClassifyData",
          method:"POST",
          header:{
            "content-type":"application/x-www-form-urlencoded"
          },
          data:{
            classifyCode:this.data.filecode,
            projectCode:this.data.projectCode
          }
    
        }).then(v=>{
          if(!v.code==200){
            wx.showToast({
              title: v.msg,

            })

          }else{
            v.data.content.forEach(item=>{
              if(item.type=='image'){
                image.push({
                  source:'image',
                  url:item.url
                })
              }
            })
            this.setData({
              content:v.data.content,
              image
            })
          }
        })
    },
  },
  lifetimes:{
    attached:function(){
      this.getListFile()
      wx.pageScrollTo({
        scrollTop:0
      })
    }
  }
})
